/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.listeners;

import ftpcombiner.ApplicationController;
import ftpcombiner.filesystem.FileSystem;
import ftpcombiner.gui.applicationframe.framesplitter.FileTable;
import ftpcombiner.server.serverconnection.File;
import ftpcombiner.server.virtualfiles.VirtualFile;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFileChooser;

/**
 *
 * @author Ben
 */
public class DownloadListener extends MouseAdapter {

    JFileChooser downloadChooser;
    FileTable parent;

    public DownloadListener() {
        downloadChooser = new JFileChooser();
        downloadChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (parent == null) {
            parent = ApplicationController.getInstance().getApplicationFrame().getFrameSplitter().getFileTable();
        }
        if (parent.getValueAt(parent.getSelectedRow(), 0) instanceof File) {
            downloadChooser.showDialog(parent, "Select");
            try {
                for (int i:parent.getSelectedRows()) {
                    System.out.println(parent.getValueAt(i, 0)+":"+parent.getValueAt(i, 0).getClass().getSimpleName());
                    FileSystem.getInstance().download((VirtualFile) parent.getValueAt(i, 0), new java.io.File(downloadChooser.getSelectedFile().toPath().toString() + "\\" + (parent.getValueAt(parent.getSelectedRow(), 0))));
                }
            } catch (NullPointerException ex) {
            }
        } else {
            ApplicationController.getInstance().setAppStatus("Cannot download folder");
        }
    }
}
